package com.xintonghua.bussiness.abcpinying;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.ListView;
import android.widget.TextView;


import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/7/13.
 */

public class PinyinDemoActivity extends BaseActivity {

    private ListView sortListView;
    private SideBar sideBar;
    private TextView dialog;
    private ChoiceCrewAdapter adapter;
    private ClearEditText mClearEditText;
//    private Button Finish_btn;
    private Message message;
    private Bundle bundle;

    /**
     * 汉字转换成拼音的类
     */
    private CharacterParser characterParser;
    private List<SortModel> SourceDateList;
    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;
    private List<SortModel> list;
    Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.abc_activity_pinyindemo);
        initViews();
        initInterNet();
    }

    private void initViews() {

        // 实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();
        pinyinComparator = new PinyinComparator();
        sideBar = (SideBar) findViewById(R.id.sidrbar);
        dialog = (TextView) findViewById(R.id.dialog);
//        Finish_btn = (Button) findViewById(R.id.Finish_btn);
        SourceDateList = new ArrayList<SortModel>();
        sideBar.setTextView(dialog);

        // 设置右侧触摸监听
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                // 该字母首次出现的位置
                int position = adapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    sortListView.setSelection(position);
                }
            }
        });

        // 更新了数据
        initData();
        // 根据a-z进行排序源数据
        Collections.sort(SourceDateList, pinyinComparator);
        adapter = new ChoiceCrewAdapter(this, SourceDateList);
        sortListView = (ListView) findViewById(R.id.country_lvcountry);
        sortListView.setAdapter(adapter);
        mClearEditText = (ClearEditText) findViewById(R.id.filter_edit);
        // 根据输入框输入值的改变来过滤搜索
        mClearEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // 当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
                filterData(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
//        this.findViewById(R.id.return_btn).setOnClickListener(
//                new View.OnClickListener() {
//
//                    @Override
//                    public void onClick(View v) {
//                        PinyinDemoActivity.this.finish();
//                    }
//                });
    }

    private void initInterNet() {
        list = new ArrayList<>();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    SortModel model = new SortModel();
                    model.setNickName("水家乐福" + i);
                    model.setMotto("描述" + i);
                    list.add(model);
                }

                for (int i = 0; i < 50; i++) {
                    SortModel model = new SortModel();
                    model.setNickName("流口水家乐福" + i);
                    model.setMotto("描述" + i);
                    list.add(model);
                }

                for (int i = 0; i < 50; i++) {
                    SortModel model = new SortModel();
                    model.setNickName("都流口水家乐福" + i);
                    model.setMotto("描述" + i);
                    list.add(model);
                }


                for (int i = 0; i < 50; i++) {
                    SortModel model = new SortModel();
                    model.setNickName("你好" + i);
                    model.setMotto("描述" + i);
                    list.add(model);
                }
                Updata();
            }
        }, 1000);


    }

    protected void Updata() {
        System.out.println("执行更新数据");
        SourceDateList.addAll(filledData(list));
        Collections.sort(SourceDateList, pinyinComparator);
        adapter.notifyDataSetChanged();
    }


    protected void initData() {
        SourceDateList.addAll(filledData(list));
    }

    /**
     * 为ListView填充数据
     * @param date
     * @return
     */
    private List<SortModel> filledData(List<SortModel> date) {
        List<SortModel> mSortList = new ArrayList<SortModel>();
        if (date != null && !date.isEmpty()) {
            System.out.println("不为空");
            for (int i = 0; i < date.size(); i++) {
                SortModel sortModel = new SortModel();
                sortModel.setNickName(date.get(i).getNickName());
                sortModel.setId(date.get(i).getId());

                // 汉字转换成拼音
                String pinyin = characterParser.getSelling(date.get(i)
                        .getNickName());
                String sortString = pinyin.substring(0, 1).toUpperCase();

                // 正则表达式，判断首字母是否是英文字母
                if (sortString.matches("[A-Z]")) {
                    sortModel.setSortLetters(sortString.toUpperCase());
                } else {
                    sortModel.setSortLetters("#");
                }
                sortModel.setMotto("这只是一项测试");

                mSortList.add(sortModel);
            }
        } else {
            System.out.println("为空");
        }
        return mSortList;
    }
    /**
     * 根据输入框中的值来过滤数据并更新ListView
     *
     * @param filterStr
     */
    private void filterData(String filterStr) {
        List<SortModel> filterDateList = new ArrayList<SortModel>();
        // 是否为空
        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = SourceDateList;
        } else {
            filterDateList.clear();
            for (SortModel sortModel : SourceDateList) {
                String name = sortModel.getNickName();
                if (name.indexOf(filterStr.toString()) != -1
                        || characterParser.getSelling(name).startsWith(
                        filterStr.toString())) {
                    filterDateList.add(sortModel);
                }
            }
        }

        // 根据a-z进行排序
        Collections.sort(filterDateList, pinyinComparator);
        adapter.updateListView(filterDateList);
    }

    public ListView getSortListView() {
        return sortListView;
    }

    public void setSortListView(ListView sortListView) {
        this.sortListView = sortListView;
    }

    public ChoiceCrewAdapter getAdapter() {
        return adapter;
    }

    public void setAdapter(ChoiceCrewAdapter adapter) {
        this.adapter = adapter;
    }

}